package com.hp.bon.sgw.wh.utils;

import com.alibaba.fastjson.JSONObject;
import com.hp.bon.sgw.domain.TBusinessRecordApi;
import com.hp.bon.sgw.threads.MessageDetailCacheManager;
import com.hp.bon.sgw.wh.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.simpleframework.http.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.Date;

public class LogUtil {
    private static final Logger logger= LoggerFactory.getLogger(LogUtil.class);


    /**
     * 日志信息保存到数据库
     * @param request
     * @param resultJson 业务返回
     * @param startTime
     */
    public static void saveLogToABM(Request request, String resultJson,long startTime){
        try {
            JSONObject jsonObject = JSONObject.parseObject(resultJson);
            String resultCode =jsonObject.getString("resultCode");
            String IP = request.getClientAddress().getAddress().getHostAddress();
            if(IP == null){ IP="";}
            String reqPath = request.getPath().getPath();
            String CTGRequestID = request.getValue("X-CTG-Request-ID");
            long endTime = Calendar.getInstance().getTimeInMillis();
            TBusinessRecordApi api=new TBusinessRecordApi();
            api.setId((int)(Math.random()*1000000000));
            api.setBusiness_call_id(DateUtil.DateToStr(new Date(),"yyyyMMddHHmmssSSS")+(int)(Math.random()*1000000000));
            api.setSession_id(CTGRequestID);
            api.setRecv_request_time(DateUtil.DateToStr(new Date(startTime),"yyyyMMddHHmmssSSS"));
            api.setSend_response_time(DateUtil.DateToStr(new Date(),"yyyyMMddHHmmssSSS"));
            api.setService_context_id(reqPath);
            api.setTrans_direction(Integer.valueOf((endTime-startTime)+""));
            api.setRESULT_REASON(resultCode);
            api.setRequest_ip(IP);
            MessageDetailCacheManager.getInstance().add(api);
            logger.info("=================CTGRequestID:"+CTGRequestID
                    +"=========recv_request_time:"+DateUtil.DateToStr(new Date(startTime),"yyyyMMddHHmmssSSS")
                    +"=========send_response_time:"+DateUtil.DateToStr(new Date(),"yyyyMMddHHmmssSSS")
                    +"=========trans_direction:"+(endTime-startTime)
                    +"========+service_context_id"+reqPath);
        }catch (Exception e){
            logger.error("=========================保存到出数据库错误=======",e);
        }

    }

    /**
     * 日志信息保存到数据库
     * @param request
     * @param resultJson 业务返回
     * @param startTime
     */
    public static void saveLogToABM(Request request, String resultJson,long startTime,JSONObject requestObject){
        try {
            JSONObject jsonObject = JSONObject.parseObject(resultJson);
            String resultCode =jsonObject.getString("resultCode");
            String IP = request.getClientAddress().getAddress().getHostAddress();
            if(IP == null){ IP="";}
            String reqPath = request.getPath().getPath();
            String CTGRequestID = request.getValue("X-CTG-Request-ID");
            long endTime = Calendar.getInstance().getTimeInMillis();
            TBusinessRecordApi api=new TBusinessRecordApi();
            api.setId((int)(Math.random()*1000000000));
            api.setBusiness_call_id(DateUtil.DateToStr(new Date(),"yyyyMMddHHmmssSSS")+(int)(Math.random()*1000000000));
            api.setSession_id(CTGRequestID);
            api.setRecv_request_time(DateUtil.DateToStr(new Date(startTime),"yyyyMMddHHmmssSSS"));
            api.setSend_response_time(DateUtil.DateToStr(new Date(),"yyyyMMddHHmmssSSS"));
            api.setService_context_id(reqPath);
            api.setTrans_direction(Integer.valueOf((endTime-startTime)+""));
            api.setRESULT_REASON(resultCode);
            api.setRequest_ip(IP);
            if(requestObject.get("jfStartTime") != null){
                Long jfStartTime = requestObject.getLongValue("jfStartTime");
                long jfEndTime = System.currentTimeMillis();
                if(requestObject.get("jfEndTime") != null){
                     jfEndTime = requestObject.getLongValue("jfEndTime");
                }
                api.setSEND_REQUEST_TIME(DateUtil.DateToStr(new Date(jfStartTime),"yyyyMMddHHmmssSSS"));
                api.setRECV_RESPONSE_TIME(DateUtil.DateToStr(new Date(jfEndTime),"yyyyMMddHHmmssSSS"));
                //需要记录计费时间差
                api.setRECV_RESPONSE_LENGTH(Integer.valueOf((jfEndTime-jfStartTime)+""));
                if(requestObject.get("webTimeLength") != null){
                    logger.info("=================webTimeLength:"+requestObject.get("webTimeLength"));
                    //单独处理欠费查询 DCC请求
                    api.setSend_request_length(requestObject.getIntValue("webTimeLength"));
                }
            }
            MessageDetailCacheManager.getInstance().add(api);
            logger.info("=================CTGRequestID:"+CTGRequestID
                    +"=========recv_request_time:"+DateUtil.DateToStr(new Date(startTime),"yyyyMMddHHmmssSSS")
                    +"=========send_response_time:"+DateUtil.DateToStr(new Date(),"yyyyMMddHHmmssSSS")
                    +"=========trans_direction:"+(endTime-startTime)
                    +"========+service_context_id"+reqPath);
        }catch (Exception e){
            logger.error("=========================保存到出数据库错误=======",e);
        }

    }

    /**
     * TMallChannel.java
     * 日志信息保存到数据库
     */
    public static void saveLogToABM_TMall(Request request, String resultCode,long startTime){
        try {
            if(StringUtils.isEmpty(resultCode)){
                resultCode="2006-1";
            }
            String IP = request.getClientAddress().getAddress().getHostAddress();
            if(IP == null){ IP="";}
            String reqPath = request.getPath().getPath();
            String method = request.getMethod();
            String CTGRequestID = request.getValue("X-CTG-Request-ID");
            long endTime = System.currentTimeMillis();
            TBusinessRecordApi api=new TBusinessRecordApi();
            api.setId((int)(Math.random()*1000000000));
            api.setBusiness_call_id(DateUtil.DateToStr(new Date(),"yyyyMMddHHmmssSSS")+(int)(Math.random()*1000000000));
            api.setSession_id(CTGRequestID);
            api.setRecv_request_time(DateUtil.DateToStr(new Date(startTime),"yyyyMMddHHmmssSSS"));
            api.setSend_response_time(DateUtil.DateToStr(new Date(),"yyyyMMddHHmmssSSS"));
            api.setService_context_id(reqPath+"_"+method);
            api.setTrans_direction(Integer.valueOf((endTime-startTime)+""));
            api.setRESULT_REASON(resultCode);
            api.setRequest_ip(IP);
            MessageDetailCacheManager.getInstance().add(api);
            logger.info("=================CTGRequestID:"+CTGRequestID
                    +"=========recv_request_time:"+DateUtil.DateToStr(new Date(startTime),"yyyyMMddHHmmssSSS")
                    +"=========send_response_time:"+DateUtil.DateToStr(new Date(),"yyyyMMddHHmmssSSS")
                    +"=========trans_direction:"+(endTime-startTime)
                    +"========+service_context_id"+reqPath);
        }catch (Exception e){
            logger.error("=========================保存到出数据库错误=======",e);
        }

    }


}
